package cn.avalon.org.controller;

import cn.avalon.basic.utils.AjaxResult;
import cn.avalon.basic.utils.PageList;
import cn.avalon.org.domain.Department;
import cn.avalon.org.query.DepartmentQuery;
import cn.avalon.org.service.DepartmentService;
import cn.avalon.system.annotation.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "部门接口类",description = "部门接口类")
@RestController
@RequestMapping("/department")
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    /**
     * @description: 查询单个
     * @author: Avalon
     * @date: 2022/8/14 : 10:12
     * @param:
     * @return:
     */
    @ApiOperation(value = "查询单个")
    @GetMapping("/{id}")
    public Department findOne(@PathVariable("id")Long id) {
        return departmentService.findById(id);
    }

    /**
     * @description: 查询所有
     * @author: Avalon
     * @date: 2022/8/14 : 10:12
     * @param:
     * @return:
     */
    @ApiOperation(value = "查询所有")
    @GetMapping
    public List<Department> findAll() {
        return departmentService.findAll();
    }

    /**
     * @description: 添加或修改
     * @author: Avalon
     * @date: 2022/8/14 : 10:26
     * @param:
     * @return:
     */
    @ApiOperation(value = "添加或修改")
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Department department) {
        try {
            if (department.getId() == null) {
                departmentService.save(department);
            } else {
                departmentService.update(department);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "操作失败");
        }
    }

    /**
     * @description: 删除接口
     * @author: Avalon
     * @date: 2022/8/14 : 10:26
     * @param:
     * @return:
     */
    @ApiOperation(value = "删除接口")
    @DeleteMapping("/{id}")
    public AjaxResult deleteById(@PathVariable("id")Long id) {
        try {
            departmentService.remove(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "删除失败");
        }
    }

    /**
     * @description: 分页接口
     * @author: Avalon
     * @date: 2022/8/14 : 10:31
     * @param:
     * @return:
     */
    @ApiOperation(value = "分页接口")
    @PostMapping
    @PreAuthorize(name = "部门列表",sn = "department:list") //value后面用来做权限校验
    public PageList<Department> queryPage(@RequestBody DepartmentQuery departmentQuery) {
        return departmentService.queryPage(departmentQuery);
    }

    @ApiOperation(value = "批量删除接口")
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody List<Long> ids) {
        try {
            departmentService.patchDelete(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"批量删除失败");
        }
    }

    @ApiOperation(value = "查询部门树")
    @GetMapping("/deptTree")
    public List<Department> deptTree() {
        return departmentService.deptTree();
    }
}
